package what.basic.chooser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Jfilechooser implements ActionListener{
JFrame f=null;
JLabel lbl =null;
JTextArea txta=null;
JFileChooser fc=null;
public Jfilechooser(){
	f=new JFrame("FileChooserexample");
	Container cp=f.getContentPane();
	txta=new JTextArea();
	JScrollPane sp=new JScrollPane(txta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	sp.setPreferredSize(new Dimension(350,300));
	JPanel jp=new JPanel();
	JButton b1=new JButton("new File");
	b1.addActionListener(this);
	JButton b2=new JButton("save file");
	b2.addActionListener(this);
	jp.add(b1);
	jp.add(b2);
	lbl=new JLabel("",JLabel.CENTER);
	fc=new JFileChooser("D:\\");
	cp.add(lbl,BorderLayout.NORTH);
	cp.add(sp,BorderLayout.CENTER);
	cp.add(jp,BorderLayout.SOUTH);
	f.pack();
	f.setVisible(true);
	f.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
	});
}
	public static void main(String[] args) {
		new Jfilechooser();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		File file=null;
		int result;
		if(e.getActionCommand().equals("new File")){
			fc.setApproveButtonText("OK");
			fc.setDialogTitle("open file");
			result=fc.showOpenDialog(f);
			txta.setText("");
			if(result==JFileChooser.APPROVE_OPTION){
				file=fc.getSelectedFile();
				lbl.setText("you selected file:"+file.getName());
			}
			else if(result==JFileChooser.CANCEL_OPTION){
				lbl.setText("you did not select any file");
			}
		}
		FileInputStream filein=null;
		if(file!=null){
			try{
				filein=new FileInputStream(file);
			}
			catch(FileNotFoundException fe){
				lbl.setText("File not found");
				return;
			}
			int readbyte;
			try{
				while((readbyte=filein.read())!=-1){
					txta.append(String.valueOf((char)readbyte));
				}
			}
			catch(IOException ioe){
				lbl.setText("read file error");
			}
			finally{
				try{
					if(filein!=null){
						filein.close();
					}
				}
				catch(IOException ioe2){}
			}
		}
		if(e.getActionCommand().equals("save file")){
			result=fc.showSaveDialog(f);
			file=null;
			String fileName;
			if(result==JFileChooser.APPROVE_OPTION){
				file=fc.getSelectedFile();
				lbl.setText("you selected file:"+ file.getName());
			}
			else if(result==JFileChooser.CANCEL_OPTION)
				lbl.setText("you did not select file");
		}
		FileOutputStream fileout =null;
		if(file!=null){
			try{
				fileout=new FileOutputStream(file);
			}
			catch(FileNotFoundException fe){
				lbl.setText("file not found");
				return;
			}
			String content=txta.getText();
			try{
				fileout.write(content.getBytes());
			}
			catch(IOException ioe){
				lbl.setText("write file error");
			}
			finally{
				try{
					if(fileout!=null){
						fileout.close();
					}
				}
				catch(IOException ioe2){}
			}
		}
	}

}
